<?php
namespace App\Repository\Sys;

use App\Repository\Lib\Message;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;


/**
 * 一些系统操作
 * Class Sys
 * @package App\Repository\Sys
 */
class Sys
{


    //发送常规短信
    static public function SendMessage($templateid, $mobiles = [], $params = '')
    {
        $url = 'http://api.aiweiup.com/service/message/send';
        $data = array(
            'project_id'=>1,
            'templateid' => $templateid,
            'mobiles' => json_encode($mobiles),
            'params' => json_encode($params)
        );

        $res = Curl::post($url, $data);

        $res = \json_decode($res, true);

        if ($res['code'] == 1) {
            \Log::info('短信发送成功,模版id:' . $templateid . '手机号:' . \json_encode($mobiles));
            return JsonRespose::jsonArray(1, null, $res['msg']);
        } else {
            \Log::info('短信发送失败,模版id:' . $templateid . '手机号:' . \json_encode($mobiles));
            return JsonRespose::jsonArray(0, null, $res['msg']);
        }
    }


    //获取前台uid
    static public function getUid()
    {
        // 测试写死


//        return 29;
        $uid = session('uid');
        if (!$uid) {
//            return 20;
            return false;
        }
        return $uid;
    }

    //前台获取失败返回
    static public function getUidFail()
    {
        return JsonRespose::fail('用户信息无效,请重新登录', -1);
    }


    static public function toUTF8($str)
    {
        return mb_convert_encoding($str, 'UTF-8', 'GBK'); //编码转换为utf-8
    }

    static public function toGBK($str)
    {
        return mb_convert_encoding($str, 'GBK', 'UTF-8'); //编码转换为gbk
    }


    static public function isWindows()
    {
        if (PATH_SEPARATOR == ':') {
            //linux
            return false;
        } else {
            return true;
        }
    }



    /**
     *
     * 记录请求日志
     * @param string $other_str
     * @param bool $error
     * @return bool
     */
    static public function writeRequestData($other_str = '', $error = false)
    {
        //只记录POST请求和报错的请求
        if (empty($_POST) and !$error) {
            return true;
        }
        // 记录请求的数据  可以存入其他地方
        $log = new Logger('requestData');
        $log->pushHandler(
            new StreamHandler(
                storage_path('logs/request_data_' . date('Y-m-d') . '.log')
            )
        );
        $run_time = round(microtime(true) - START_TIME, 3);
        $_SERVER['REQUEST_URI'] = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
        $data = "请求地址:{$_SERVER['REQUEST_URI']} | 执行时间:{$run_time} | POST数据:" . json_encode($_POST, JSON_UNESCAPED_UNICODE) . $other_str;

        if ($error) {
            $log->addError($data);
        } else {
            $log->addInfo($data);
        }

    }
    //是否后台调用
    static public function isAdminApi(){
        if(strpos($_SERVER['REQUEST_URI'], 'adminapi') !== false){
            return true;
        }else{
            return false;
        }
    }
}

